/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.server.dl;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import com.feiyue.basic.server.cache.CacheFetch;
import com.feiyue.basic.server.db.BookCollectDBHandler;
import com.feiyue.basic.server.net.NovelInfo;
import com.feiyue.basic.server.parser.Book;
import com.feiyue.basic.server.parser.Chapter;
import java.util.List;

public class Thread2Dl extends Thread {
	private int bookid;
	private int categoryid;
	private int chapterid;
	private List<Chapter> chapterlist;
	private Context context;
	private DlRunnable dlRunnable;
	private Handler handler;
	private String issuestate;
	private long lasttime = 0L;

	public Thread2Dl(Context paramContext, DlRunnable paramDlRunnable,
			Handler paramHandler, int paramInt1, int paramInt2,
			String paramString) {
		this.dlRunnable = paramDlRunnable;
		this.handler = paramHandler;
		this.context = paramContext;
		this.categoryid = paramInt1;
		this.bookid = paramInt2;
		this.issuestate = paramString;
	}

	public void run()
    {
        chapterlist = NovelInfo.getChapterList(context, NovelInfo.getUrl2Chapterlist(bookid), categoryid, bookid, "cache first", handler);
        if (chapterlist != null && chapterlist.size() != 0) {
        	int i;
            int j;
            int k;
            i = chapterlist.size();
            j = 0;
            k = 0;
            String s;
            String s1;
            Intent intent;
            while (k < i) {
            	chapterid = ((Chapter)chapterlist.get(k)).getId();
                s = NovelInfo.getUrl2ChapterContent(bookid, chapterid);
                s1 = (new StringBuilder(String.valueOf(bookid))).append("_").append(chapterid).toString();
                if (CacheFetch.getFile(context, s, s1, true) != null)
                {
                    j++;
                    if (System.currentTimeMillis() - lasttime >= 2000L || j == i)
                    {
                        dlRunnable.setPercentage((j * 100) / i);
                        handler.post(dlRunnable);
                        lasttime = System.currentTimeMillis();
                    }
                }
                k++;
            }
            
            try
            {
                BookCollectDBHandler bookcollectdbhandler = new BookCollectDBHandler(context);
                Book book = bookcollectdbhandler.getBook(context, bookid);
                book.setdlstate(1);
                bookcollectdbhandler.set(book, context);
                bookcollectdbhandler.destroy();
            }
            catch (Exception exception) { }
            intent = new Intent("MESSAGERECEIVER_BOOKID");
            intent.putExtra("bookid", bookid);
            context.sendBroadcast(intent);
        }
    }
}